package com.exchange.otherservice;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.Date;
import java.util.Map;

import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey.Match;
import com.exchange.common.model.MatchDeal;
import com.exchange.common.model.MatchOrder;
import com.exchange.common.model.dto.Tick;
import com.exchange.common.model.dto.Trade;
import com.exchange.plugin.influx.InfluxDbKit;
import com.exchange.plugin.threadpool.ThreadPoolKit;
import com.exchange.plugin.webSocket.MyWsMsgHandler;
import com.exchange.util.KlineUtil;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;

public class KafkaConsumerService {
	
	private static Cache rides = Redis.use(PropKit.get("redisName"));
	
	@Inject
	static MyWsMsgHandler ws;
	
	/**
	 *  存k线，存流水, 发websocket
	 * @param value
	 */
	public static void insertValue(String value){
		Trade trade = JSONUtil.toBean(value, Trade.class);
		//{"isBuy":true,"uid":455951945836789761,"number":1,"dealWay":"maker","coinTeam":"XBITUSDT","totalPrice":10,"price":10,"oid":455951945836789760}
		Date d = new Date();
		ThreadPoolKit.submit(new Runnable() {
			public void run() {
				//存k线
				InfluxDbKit.insertValue(trade.getCoinTeam(), trade.getPrice(), trade.getPrice(), trade.getPrice(), trade.getPrice(), trade.getNumber());
				//存流水
				new MatchDeal().setCoinTeam(trade.getCoinTeam()).setCreateTime(d).setDealWay(trade.getDealWay())
				.setIsBuy(trade.getIsBuy()).setNumber(trade.getNumber()).setOrderNum(trade.getOid())
				.setPrice(trade.getPrice()).setTotalPrice(trade.getTotalPrice())
				.setUserId(trade.getUid()).save();
			}
		});
		//将这条交易信息处理成最新的各个时间柱信息
		KlineUtil.put(trade, d.getTime());
		for(String type : KlineUtil.getTypes()) {
			Tick tick = rides.get("newTick:"+trade.getCoinTeam()+":"+type);
			ws.sendKLine(trade.getCoinTeam(), type, Kv.create().set("tick", tick));
		}
	}
	
	
	// TODO
	public static void orderChange(String value) {
    	//(MatchOrder部分)country={"isBuy":true,"unFinishNumber":0,"surplusFrozen":0,"coinTeam":"XBITUSDT","totalPrice":10,"list":[],"uid":455954340679516161,"number":1,"decimalNumber":4,"createTime":1597819596267,"price":10,"id":455954340679516160,"state":2,"finishNumber":1,"isMarket":false}        
		JSONObject obj = JSONUtil.parseObj(value);
		ThreadPoolKit.submit(new Runnable() {
			public void run() {
				//手续费计算
			}
		});
		
		
//		ws.sendEntrust(, Kv.create().getAs(tick));
	}
	
	public static void revokeOrder(String value) {
		//{"isBuy":true,"unFinishNumber":1,"surplusFrozen":10,"coinTeam":"XBIT-USDT2","totalPrice":10,"list":[],"uid":456419994738098177,"number":1,"decimalNumber":4,"createTime":1597930616846,"price":10,"id":456419994738098176,"state":0,"finishNumber":0,"isMarket":false}
		JSONObject obj = JSONUtil.parseObj(value);
		ThreadPoolKit.submit(new Runnable() {
			public void run() {
				//手续费计算
			}
		});
	}
	
	public static void tradeDepth(String value) {
    	//(Depth)country={"buy":[{"isBuy":true,"number":"0","total":"0","coinTeam":"XBIT-USDT","price":"0","platform":1}],"sell":[{"isBuy":false,"number":"0","total":"0","coinTeam":"XBIT-USDT","price":"0","platform":1}]}
        
		JSONObject obj = JSONUtil.parseObj(value);
//		System.out.println(obj);
		//存redis
	}
}
